﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Besture.Common.SMS
{
    public class ChangeWeiBoUrl
    {
        /// <summary> 
        /// 从一条新浪微博地址中获取这条微博的id
        /// </summary>  
        /// <param name="url">一条微博地址</param>  
        /// <returns>微博id</returns>  
        public  string GetIdFromUrl(string url)
        {
            string mid = GetMidFromUrl(url);

            if (string.IsNullOrEmpty(mid))
            {
                return string.Empty;
            }
            else
            {
                return Mid2Id(mid);
            }
        }

        /// <summary> 
        /// 从一条新浪微博地址中获取这条微博的mid
        /// </summary>  
        /// <param name="url">一条微博地址</param>  
        /// <returns>微博mid</returns>  
        public static string GetMidFromUrl(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return "";
            }
            if (url.IndexOf('?') != -1)
            {
                url = url.Substring(0, url.IndexOf('?'));
            }
            Regex reg = new Regex(@"^http://(e\.)?weibo\.com/[0-9a-zA-Z]+/(?<id>[0-9a-zA-Z]+)$", RegexOptions.IgnoreCase);
            Match match = reg.Match(url);
            if (match.Success)
            {
                return match.Result("${id}");
            }
            return "";
        }

        /// <summary>
        /// 将新浪微博mid转换成id
        /// </summary>
        /// <param name="mid">微博mid</param>
        /// <returns>返回微博的id</returns>
        public static string Mid2Id(string mid)
        {
            string id = "";
            for (int i = mid.Length - 4; i > -4; i = i - 4) //从最后往前以4字节为一组读取URL字符
            {
                int offset1 = i < 0 ? 0 : i;
                int len = i < 0 ? mid.Length % 4 : 4;
                var str = mid.Substring(offset1, len);

                str = Str62toInt(str);
                if (offset1 > 0) //若不是第一组，则不足7位补0
                {
                    while (str.Length < 7)
                    {
                        str = "0" + str;
                    }
                }
                id = str + id;
            }

            return id;
        }

        /// <summary>
        /// 新浪微博id转换为mid
        /// </summary>
        /// <param name="id">微博id</param>
        /// <returns>返回微博的mid</returns>
        public static string Id2Mid(string id)
        {
            string mid = "", strTemp;
            int startIdex, len;

            for (var i = id.Length - 7; i > -7; i = i - 7) //从最后往前以7字节为一组读取mid
            {
                startIdex = i < 0 ? 0 : i;
                len = i < 0 ? id.Length % 7 : 7;
                strTemp = id.Substring(startIdex, len);
                mid = IntToStr62(Convert.ToInt32(strTemp)) + mid;
            }
            return mid;
        }

        //62进制转成10进制
        public static string Str62toInt(string str62)
        {
            Int64 i64 = 0;
            for (int i = 0; i < str62.Length; i++)
            {
                Int64 Vi = (Int64)Math.Pow(62, (str62.Length - i - 1));
                char t = str62[i];
                i64 += Vi * GetInt10(t.ToString());
            }
            return i64.ToString();
        }
        //10进制转成62进制
        public static string IntToStr62(int int10)
        {
            string s62 = "";
            int r = 0;
            while (int10 != 0)
            {
                r = int10 % 62;
                s62 = Get62key(r) + s62;
                int10 = int10 / 62;
            }
            return s62;
        }
        // 62进制字典
        private static string str62keys = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVTXYZ";
        //获取key对应的62进制整数  
        private static Int64 GetInt10(string key)
        {
            return str62keys.IndexOf(key);
        }
        //获取62进制整数对应的key
        private static string Get62key(int int10)
        {
            if (int10 < 0 || int10 > 61)
                return "";
            return str62keys.Substring(int10, 1);
        }
    }
}
